package jacksondemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import jacksondemo.bean.Home;
import jacksondemo.bean.Item;
import jacksondemo.bean.Person;

/**
 * @author rensiyu
 **/
@RestController
@RequestMapping("/json")
public class TestController {
	
	public Home getHome() {
		Map<String, Item> itemMap1 = new HashMap<>();
		Map<String, Item> itemMap2 = new HashMap<>();
		List<Person> personList = new ArrayList<>();
		Item item1 = new Item("111", "鼠标");
		Item item2 = new Item("112", "键盘");
		Item item3 = new Item("113", "money");
		itemMap1.put("111", item1);
		itemMap1.put("112", item2);
		itemMap2.put("113", item3);
		Person person1 = new Person("张三", 11, itemMap1, "郑州大学");
		Person person2 = new Person("李四", 12, itemMap2, "郑州大学");
		personList.add(person1);
		personList.add(person2);
		Home home = new Home("我的家", "河南商丘", personList);
		return home;
	}
	
	
	@GetMapping("/get")
	public String getJson() {
		ObjectMapper objectMapper = new ObjectMapper();
		String s = null;
		try {
			s = objectMapper.writeValueAsString(getHome());
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return s;
	}
}
